//
//  MyChangeSecrectViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/9/23.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "MyChangeSecrectViewController.h"

@interface MyChangeSecrectViewController ()<UITextFieldDelegate>

@property(nonatomic,strong)UITextField *origialTextfield;
@property(nonatomic,strong)UITextField *freshTextfield;
@property(nonatomic,strong)UITextField *confirmTextfield;

@end

@implementation MyChangeSecrectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self createNavagationBackBtn];
    self.view.backgroundColor = ZColorRGB(238, 238, 238);
    self.title = @"修改密码";
    [self createUI];
}
//-(void)createNavagationBackBtn{
//    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    backBtn.frame = CGRectMake(0, 6, 67, 23);
//    UIImageView *backImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigator_btn_back"]];
//    backImg.frame = CGRectMake(-10, 0, 22, 22);
//    [backBtn addSubview:backImg];
//    UILabel *backText = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 65, 22)];
//    backText.text = @"返回";
//    backText.font = [UIFont systemFontOfSize:17];
//    [backText setBackgroundColor:[UIColor clearColor]];
//    [backText setTextColor:[UIColor whiteColor]];
//    [backBtn addSubview:backText];
//    [backBtn addTarget:self action:@selector(leftBarButtonItemPressed:) forControlEvents:UIControlEventTouchUpInside];
//    [backImg mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(backBtn.mas_left).offset(-10);
//        make.centerY.equalTo(backBtn.mas_centerY);
//        make.size.mas_equalTo(CGSizeMake(22, 22));
//    }];
//    [backText mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(backImg.mas_right).offset(2);
//        make.centerY.equalTo(backBtn.mas_centerY);
//        make.size.mas_equalTo(CGSizeMake(65, 22));
//    }];
//    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
//    [self.navigationItem setLeftBarButtonItem:leftButton];
//}
//-(void)leftBarButtonItemPressed:(UIButton *)btn{
//    [self dismissViewControllerAnimated:YES completion:nil];
//}
-(void)createUI{
    UIView *topView = [self backUIAccoringBgviewAndTitleviewAndTextfield:self.origialTextfield AndLabeltext:@"原密码" AndTextfieldplaceholder:@"原密码"];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(66);
        make.height.mas_equalTo(scaleTopAndBottom(40));
    }];
    UIView *middleView = [self backUIAccoringBgviewAndTitleviewAndTextfield:self.freshTextfield AndLabeltext:@"新密码" AndTextfieldplaceholder:@"新密码"];
    [middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(topView.mas_bottom).offset(scaleTopAndBottom(10));
        make.height.mas_equalTo(scaleTopAndBottom(40));
    }];
    UIView *bottomView = [self backUIAccoringBgviewAndTitleviewAndTextfield:self.confirmTextfield AndLabeltext:@"验证" AndTextfieldplaceholder:@"验证密码"];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(middleView.mas_bottom).offset(scaleTopAndBottom(10));
        make.height.mas_equalTo(scaleTopAndBottom(40));
    }];
    UILabel *instroLabel = [[UILabel alloc] init];
    instroLabel.text = @"密码由6-20位英文字母、数字或符号组成";
    [self.view addSubview:instroLabel];
    [instroLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(10));
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-10));
        make.top.equalTo(bottomView.mas_bottom).offset(scaleTopAndBottom(5));
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:submitBtn];
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [submitBtn setBackgroundColor:[UIColor orangeColor]];
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(changeTheword:) forControlEvents:UIControlEventTouchUpInside];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(instroLabel.mas_bottom).offset(scaleTopAndBottom(40));
        make.centerX.equalTo(self.view.mas_centerX);
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(40));
        make.height.mas_equalTo(scaleTopAndBottom(50));
    }];
    
}
-(void)changeTheword:(UIButton *)btn{
    if ([self.confirmTextfield.text isEqual:@""] || [self.origialTextfield.text isEqual:@""] || [self.freshTextfield.text isEqual:@""]) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"信息填写不完整" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }else{
        if (![self.confirmTextfield.text isEqual:self.freshTextfield.text]) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"两次密码输入不一致" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        }else{
            AFHTTPSessionManager *manage = [AFHTTPSessionManager manager];
            manage.responseSerializer = [AFHTTPResponseSerializer serializer];
            NSString *url = ServerIp@"/customerApp/modifyPassWord";
            NSString *sessionId = [[NSUserDefaults standardUserDefaults] objectForKey:@"sessionId"];
            NSDictionary *param = @{@"formerPassWord":self.origialTextfield.text,@"newPassWord":self.freshTextfield.text,@"sessionId":sessionId};
            [manage POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                NSLog(@"%@",jsonDic[@"msg"]);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
            }];
        }

    }
    
}

-(UIView *)backUIAccoringBgviewAndTitleviewAndTextfield:(UITextField *)textfield AndLabeltext:(NSString *)labelStr AndTextfieldplaceholder:(NSString *)placeStr{
    UIView * topView = [[UIView alloc] init];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = labelStr;
    [topView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(topView.mas_centerY);
        make.left.equalTo(topView.mas_left).offset(scaleLeftAndRight(10));
        make.size.mas_equalTo(CGSizeMake(scaleTopAndBottom(100), scaleTopAndBottom(30)));
    }];
    textfield.placeholder = placeStr;
    [topView addSubview:textfield];
    [textfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(topView.mas_centerY);
        make.left.equalTo(titleLabel.mas_right).offset(scaleLeftAndRight(10));
        make.right.equalTo(topView.mas_right).offset(scaleLeftAndRight(-10));
        make.height.mas_equalTo(scaleTopAndBottom(30));
    }];
    return topView;
}

-(UITextField *)origialTextfield{
    if (_origialTextfield == nil) {
        _origialTextfield = [[UITextField alloc] init];
        _origialTextfield.delegate = self;
    }
    return _origialTextfield;
}
-(UITextField *)freshTextfield{
    if (_freshTextfield == nil) {
        _freshTextfield = [[UITextField alloc] init];
        _freshTextfield.delegate = self;
    }
    return _freshTextfield;
}
-(UITextField *)confirmTextfield{
    if (_confirmTextfield == nil) {
        _confirmTextfield = [[UITextField alloc] init];
        _confirmTextfield.delegate = self;
    }
    return _confirmTextfield;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
